<?php


namespace app\common\model;


use think\Model;

class PhotoAlbumModel extends Model
{
    protected $name = 'photo_album';

    /**
     * 获取分页列表
     * @param int $user_id
     * @param string $date 例: 2023-01-01/2023-12-31
     * @param int $page
     * @param int $limit 数据条数
     * @return array
     */
    public function getPageList($user_id, $date, $page = 1, $limit = 5): array
    {
        try {
            $where = [
                'status' => 1
            ];
            $field = ['id','date','content','images','createtime'];
            $order = [
                'date' => 'desc',
            ];
            if(!empty($date)) {
                $start = explode('/',$date)[0];
                $end = explode('/',$date)[1];
                $where['date'] = ['between',[$start,$end]];
            }
            $list = $this
                ->where($where)
                ->field($field)
                ->order($order)
                ->paginate($limit, false, ['page' => $page]);
            foreach ($list as $row) {
                $row->createtime = datetime($row->createtime,'Y-m-d H:i');
                $row->images = wdsxh_full_url($row->images,true);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    /**
     * 获取详情
     * @param int $id
     * @return array
     */
    public function getDetail($id): array
    {
        try {
            $data = $this->get($id);
            if($data) {
                $data->createtime = datetime($data->createtime,'Y-m-d H:i');
                $data->images = wdsxh_full_url($data->images,true);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


}